package com.swz.dcrm.adpter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.LayoutRes;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.makeramen.roundedimageview.RoundedImageView;
import com.swz.dcrm.R;
import com.swz.dcrm.base.ViewHolder;
import com.swz.dcrm.model.ClueRecord;
import com.swz.dcrm.util.DateUtils;
import com.swz.dcrm.util.Tool;
import com.xh.baselibrary.widget.CircleImageView;


import java.util.Date;
import java.util.List;

import ezy.ui.view.RoundButton;

public class TodayPlanAdapter extends CustomAdapter<ClueRecord> {

    private int status;
    private OnButtonClick onButtonClick;
    private Drawable drawableRed;
    private Drawable drawableOrg;
    private Drawable drawableBlue;
    private Drawable drawableDark;
    private Drawable drawableGreen;
    public interface   OnButtonClick{
        void onLogging(ClueRecord clueRecord);
        void onCall(String phone);
        void onHandUp(long clueId);
    }

    public void setOnButtonClick(OnButtonClick onButtonClick) {
        this.onButtonClick = onButtonClick;
    }

    public TodayPlanAdapter(Context context, List<ClueRecord> datas, @LayoutRes int layout, int status) {

        super(context, layout, datas);
        this.status=status;
        drawableBlue=context.getResources().getDrawable(R.drawable.shape_circle_blue);
        drawableOrg=context.getResources().getDrawable(R.drawable.shape_circle_orange);
        drawableRed=context.getResources().getDrawable(R.drawable.shape_circle_red);
        drawableGreen=context.getResources().getDrawable(R.drawable.shape_circle_green);
        drawableDark=context.getResources().getDrawable(R.drawable.shape_circle_gray);
    }
    @Override
    protected void convert(ViewHolder holder, ClueRecord clueRecord, int position) {
        switch (status){
            case 5:
            case 6:
            case 7:
            case 8:
                holder.setText(R.id.tv_create, mContext.getString(R.string.item_create,DateUtils.dateFormat(clueRecord.getBsClueVO().getCreateTime(),"yyyy-MM-dd")));
                holder.getView(R.id.ll_user).setVisibility(View.VISIBLE);
                if(clueRecord.getBsClueVO().getMortgage()!=null&&clueRecord.getBsClueVO().getMortgage()){
                    holder.getView(R.id.tv_loan).setVisibility(View.VISIBLE);
                }else {
                    holder.getView(R.id.tv_loan).setVisibility(View.GONE);
                }
                if(clueRecord.getBsClueVO().getReplacementCar()!=null&&clueRecord.getBsClueVO().getReplacementCar()){
                    holder.getView(R.id.tv_substitution).setVisibility(View.VISIBLE);
                }else {
                    holder.getView(R.id.tv_substitution).setVisibility(View.GONE);
                }
                break;
        }
        switch (status){
            case 1:
            case 4:

                holder.getView(R.id.btn_hand_up).setVisibility(View.GONE);
                break;
            case 2:
                holder.getView(R.id.btn_hand_up).setVisibility(View.GONE);
                holder.getView(R.id.btn_logging).setVisibility(View.GONE);
                Date date= DateUtils.dateParse(clueRecord.getNextFollowTime(),"yyyy-MM-dd HH:mm:ss");
                if(date.before(new Date())){
                    holder.setText(R.id.tv_status,mContext.getString(R.string.item_overtime));
                }
                break;
            case 3:
                if(clueRecord.getNextFollowStatus()==1){
                    holder.setText(R.id.tv_fail_times,mContext.getResources().getString(R.string.item_fail_times,clueRecord.getNextFollowFailTimes()));
                }else {
                    holder.setText(R.id.tv_fail_times,mContext.getResources().getString(R.string.item_fail_times,clueRecord.getFollowFailTimes()));
                }

                break;
            case 5:
                holder.getView(R.id.c_step).setVisibility(View.VISIBLE);
                if(clueRecord.getBsClueVO().getLatestFollowTime()!=null){
                    holder.setText(R.id.tv_date1,DateUtils.dateFormat(clueRecord.getBsClueVO().getLatestFollowTime(),"MM-dd"));
                }else {
                    holder.setText(R.id.tv_date1,"");
                }
                if(clueRecord.getBsClueVO().getLatestComeShopTime()!=null){
                    holder.setText(R.id.tv_date2,DateUtils.dateFormat(clueRecord.getBsClueVO().getLatestComeShopTime(),"MM-dd"));
                }else {
                    holder.setText(R.id.tv_date2,"");
                }
                if(clueRecord.getBsClueVO().getLatestTestDriveTime()!=null){
                    holder.setText(R.id.tv_date3,DateUtils.dateFormat(clueRecord.getBsClueVO().getLatestTestDriveTime(),"MM-dd"));
                }else {
                    holder.setText(R.id.tv_date3,"");
                }
                if(clueRecord.getBsClueVO().getLatestQuotePriceTime()!=null){
                    holder.setText(R.id.tv_date4,DateUtils.dateFormat(clueRecord.getBsClueVO().getLatestQuotePriceTime(),"MM-dd"));
                }else {
                    holder.setText(R.id.tv_date4,"");
                }
                if(clueRecord.getBsClueVO().getFollowTimes()>0){

                    holder.getView(R.id.tv_state1).setBackgroundResource(R.drawable.shape_circle_blue);
                }else {
                    holder.getView(R.id.tv_state1).setBackgroundResource(R.drawable.shape_circle_gray);
                }
                if(clueRecord.getBsClueVO().getComeShopTimes()>0){
                    holder.getView(R.id.tv_state2).setBackgroundResource(R.drawable.shape_circle_blue);
                }else {
                    holder.getView(R.id.tv_state2).setBackgroundResource(R.drawable.shape_circle_gray);
                }
                if(clueRecord.getBsClueVO().getTestDriveTimes()>0){
                    holder.getView(R.id.tv_state3).setBackgroundResource(R.drawable.shape_circle_blue);
                }else {
                    holder.getView(R.id.tv_state3).setBackgroundResource(R.drawable.shape_circle_gray);
                }
                if(clueRecord.getBsClueVO().getQuotePriceTimes()>0){
                    holder.getView(R.id.tv_state4).setBackgroundResource(R.drawable.shape_circle_blue);
                }else {
                    holder.getView(R.id.tv_state4).setBackgroundResource(R.drawable.shape_circle_gray);
                }
                holder.setText(R.id.tv_state1,clueRecord.getBsClueVO().getFollowTimes()+"");
                holder.setText(R.id.tv_state2,clueRecord.getBsClueVO().getComeShopTimes()+"");
                holder.setText(R.id.tv_state3,clueRecord.getBsClueVO().getTestDriveTimes()+"");
                holder.setText(R.id.tv_state4,clueRecord.getBsClueVO().getQuotePriceTimes()+"");
                break;
            case 6:
                break;
            case 7:
                holder.getView(R.id.c_step).setVisibility(View.VISIBLE);
                holder.getView(R.id.tv_last_visit).setVisibility(View.GONE);
                holder.getView(R.id.tv_next_visit).setVisibility(View.GONE);
                holder.getView(R.id.tv_fail_reason).setVisibility(View.VISIBLE);
                ConstraintLayout.LayoutParams layoutParams= (ConstraintLayout.LayoutParams) holder.getView(R.id.c_step).getLayoutParams();
                layoutParams.topToBottom=R.id.tv_fail_reason;
                holder.getView(R.id.c_step).setLayoutParams(layoutParams);
                if(clueRecord.getBsClueVO().getLatestFollowTime()!=null){
                    holder.setText(R.id.tv_date1,DateUtils.dateFormat(clueRecord.getBsClueVO().getLatestFollowTime(),"MM-dd"));
                }else {
                    holder.setText(R.id.tv_date1,"");
                }
                if(clueRecord.getBsClueVO().getLatestComeShopTime()!=null){
                    holder.setText(R.id.tv_date2,DateUtils.dateFormat(clueRecord.getBsClueVO().getLatestComeShopTime(),"MM-dd"));
                }else {
                    holder.setText(R.id.tv_date2,"");
                }
                if(clueRecord.getBsClueVO().getLatestTestDriveTime()!=null){
                    holder.setText(R.id.tv_date3,DateUtils.dateFormat(clueRecord.getBsClueVO().getLatestTestDriveTime(),"MM-dd"));
                }else {
                    holder.setText(R.id.tv_date3,"");
                }
                if(clueRecord.getBsClueVO().getLatestQuotePriceTime()!=null){
                    holder.setText(R.id.tv_date4,DateUtils.dateFormat(clueRecord.getBsClueVO().getLatestQuotePriceTime(),"MM-dd"));
                }else {
                    holder.setText(R.id.tv_date4,"");
                }
                if(clueRecord.getBsClueVO().getFollowTimes()>0){

                    holder.getView(R.id.tv_state1).setBackgroundResource(R.drawable.shape_circle_blue);
                }else {
                    holder.getView(R.id.tv_state1).setBackgroundResource(R.drawable.shape_circle_gray);
                }
                if(clueRecord.getBsClueVO().getComeShopTimes()>0){
                    holder.getView(R.id.tv_state2).setBackgroundResource(R.drawable.shape_circle_blue);
                }else {
                    holder.getView(R.id.tv_state2).setBackgroundResource(R.drawable.shape_circle_gray);
                }
                if(clueRecord.getBsClueVO().getTestDriveTimes()>0){
                    holder.getView(R.id.tv_state3).setBackgroundResource(R.drawable.shape_circle_blue);
                }else {
                    holder.getView(R.id.tv_state3).setBackgroundResource(R.drawable.shape_circle_gray);
                }
                if(clueRecord.getBsClueVO().getQuotePriceTimes()>0){
                    holder.getView(R.id.tv_state4).setBackgroundResource(R.drawable.shape_circle_blue);
                }else {
                    holder.getView(R.id.tv_state4).setBackgroundResource(R.drawable.shape_circle_gray);
                }
                holder.setText(R.id.tv_state1,clueRecord.getBsClueVO().getFollowTimes()+"");
                holder.setText(R.id.tv_state2,clueRecord.getBsClueVO().getComeShopTimes()+"");
                holder.setText(R.id.tv_state3,clueRecord.getBsClueVO().getTestDriveTimes()+"");
                holder.setText(R.id.tv_state4,clueRecord.getBsClueVO().getQuotePriceTimes()+"");

                break;
            case 8:
                holder.getView(R.id.tv_authorized_date).setVisibility(View.VISIBLE);
                holder.getView(R.id.tv_authorized_man).setVisibility(View.VISIBLE);
                break;

        }
        holder.setText(R.id.tv_name,clueRecord.getBsClueVO().getCusName());
        holder.setText(R.id.tv_car_type,clueRecord.getBsClueVO().getIntentCar());
        if(clueRecord.getFollowType()!=0){
            holder.setText(R.id.tv_last_visit,mContext.getResources().getString(R.string.item_last_visit,DateUtils.dateFormat(clueRecord.getCreateTime(),"yyyy-MM-dd"),clueRecord.getFollowTypeDesc()));
        }else {
            holder.setText(R.id.tv_last_visit,mContext.getResources().getString(R.string.item_last_visit,"尚未填写",""));
        }
        if(clueRecord.getNextFollowTime()!=null){
            holder.setText(R.id.tv_next_visit,mContext.getResources().getString(R.string.item_next_visit,DateUtils.dateFormat(clueRecord.getNextFollowTime(),"yyyy-MM-dd"),
                    clueRecord.getNextFollowTypeDesc()));
        }else {
            holder.setText(R.id.tv_next_visit,mContext.getResources().getString(R.string.item_next_visit,"尚未填写",""));
        }

        switch (status){
            case 1:
            case 2:
            case 3:
            case 4:
                holder.setOnClickListener(R.id.btn_hand_up,v -> {

                    if(onButtonClick!=null){
                        onButtonClick.onHandUp(clueRecord.getBsClueVO().getId());
                    }
                });
                holder.setOnClickListener(R.id.btn_logging,v -> {
                    if(onButtonClick!=null){
                        onButtonClick.onLogging(clueRecord);
                    }
                });
                holder.setOnClickListener(R.id.btn_call,v -> {
                    if(onButtonClick!=null){
                        onButtonClick.onCall(clueRecord.getBsClueVO().getCusPhone());
                    }
                });

                break;
        }
        TextView level=holder.getView(R.id.tv_level);
        switch (clueRecord.getBsClueVO().getLevel()){
            case 1:
                level.setBackground(drawableRed);
                break;
            case 2:
                level.setBackground(drawableOrg);
                break;
            case 3:
                level.setBackground(drawableGreen);

                break;
            case 4:
                level.setBackground(drawableBlue);
                break;
        }
        if(clueRecord.getBsClueVO().getLevelDesc()!=null){
            level.setText(clueRecord.getBsClueVO().getLevelDesc().substring(0,1));
        }

            holder.setText(R.id.tv_sex,clueRecord.getBsClueVO().sexDesc());


        holder.setText(R.id.tv_fail_reason,mContext.getString(R.string.item_fail_reason,clueRecord.getBsClueVO().getDefeatedReason()));

        holder.itemView.setOnClickListener(v -> {
            setCurrentClickPosition(position);
            if(onClickListener!=null){
                onClickListener.onItemClick(clueRecord);
            }
        });


    }
}
